/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.controller;

import com.minierp.dao.DepartmentDao;
import com.minierp.dao.IdGenerator;
import com.minierp.dao.StorehouseDao;
import com.minierp.dao.WorkCenterDao;
import com.minierp.helper.EncCommonHelper;
import com.minierp.model.Department;
import com.minierp.model.WorkCenter;
import com.minierp.model.WorkProcess;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

public class WorkCenterFormCtr extends SimpleFormController {
	private WorkCenterDao workCenterDao;
	private StorehouseDao storehouseDao;
	private DepartmentDao departmentDao;
	private IdGenerator idGenerator;

	public DepartmentDao getDepartmentDao() {
		return this.departmentDao;
	}

	public void setDepartmentDao(DepartmentDao departmentDao) {
		this.departmentDao = departmentDao;
	}

	protected Map referenceData(HttpServletRequest request) throws Exception {
		HashMap refData = new HashMap();
		String id = request.getParameter("id");
		if (id == null) {
			id = "";
		}

		refData.put("id", id);
		refData.put("storehouseList", this.storehouseDao.getList(true));
		return refData;
	}

	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		String id = request.getParameter("id");
		new WorkCenter();
		WorkCenter form;
		if (id != null && !id.equals("")) {
			form = (WorkCenter) this.workCenterDao.get(WorkCenter.class,
					Long.parseLong(id));
		} else {
			form = new WorkCenter();
		}

		return form;
	}

	protected void onBind(HttpServletRequest request, Object command)
			throws Exception {
	}

	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		ModelAndView mv = new ModelAndView(this.getSuccessView());
		String id = request.getParameter("id");
		WorkCenter obj = (WorkCenter) command;
		String alertMsg = "";
		if (id != null && !id.equals("")) {
			this.workCenterDao.update(obj);
			alertMsg = "修改成功！";
		} else {
			obj.setId(this.idGenerator.getId(WorkCenter.class.getName()));
			this.workCenterDao.save(obj);
			List depList = this.departmentDao.getList(true);
			if (!depList.isEmpty()) {
				Department dep = (Department) depList.get(0);
				WorkProcess wp = new WorkProcess();
				wp.setDepId(dep.getId());
				wp.setId(this.idGenerator.getId(WorkProcess.class.getName()));
				wp.setPercent(new BigDecimal(1));
				wp.setProcessName("默认工序");
				wp.setSequenceCode(1);
				wp.setWorkCenterId(obj.getId());
				this.workCenterDao.save(wp);
			}

			alertMsg = "增加成功！";
		}

		mv.addObject("returnValue", String.valueOf(obj.getId()));
		EncCommonHelper.setAlertMessage(request, alertMsg);
		return mv;
	}

	public IdGenerator getIdGenerator() {
		return this.idGenerator;
	}

	public void setIdGenerator(IdGenerator idGenerator) {
		this.idGenerator = idGenerator;
	}

	public WorkCenterDao getWorkCenterDao() {
		return this.workCenterDao;
	}

	public void setWorkCenterDao(WorkCenterDao workCenterDao) {
		this.workCenterDao = workCenterDao;
	}

	public StorehouseDao getStorehouseDao() {
		return this.storehouseDao;
	}

	public void setStorehouseDao(StorehouseDao storehouseDao) {
		this.storehouseDao = storehouseDao;
	}
}